package com.yuyou.fn.educational.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.CriteriaUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.CourseOrderDiscount;
import com.yuyou.fn.educational.entity.StudentReg;
import com.yuyou.fn.educational.mapper.CourseOrderDiscountMapper;
import com.yuyou.fn.educational.service.ICourseOrderDiscountService;
import com.yuyou.fn.educational.service.IStudentRegService;
import com.yuyou.fn.educational.vo.CourseOrderDiscountQueryVo;
import com.yuyou.fn.educational.vo.DiscountCodeUseDetailVo;
import com.yuyou.fn.platform.entity.AreaTeam;
import com.yuyou.fn.platform.entity.BusTeam;
import com.yuyou.fn.platform.service.IAreaTeamService;
import com.yuyou.fn.platform.service.IBusTeamService;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 *
 * CourseOrderDiscount 表数据服务层接口实现类
 *
 */
@Service
public class CourseOrderDiscountServiceImpl extends SuperServiceImpl<CourseOrderDiscountMapper, CourseOrderDiscount,Long> implements ICourseOrderDiscountService {

    @Resource
    private IStudentRegService studentRegService;

    @Resource
    private IBusTeamService busTeamService;

    @Resource
    private IAreaTeamService areaTeamService;

    @Override
    public List<CourseOrderDiscount> findByCourseOrderIds(List<Long> courseOrderIds) {
        Example<CourseOrderDiscount> example = CourseOrderDiscount.createExample();
        example.createCriteria().andIn("courseOrderId" , courseOrderIds );
        return findListByExample(example);
    }

    @Override
    public List<CourseOrderDiscount> findByCourseOrderIdsDiscountId(List<Long> courseOrderIds, Long discountId) {
        if (Utils.checkListEmpty(courseOrderIds) || discountId==null){
            return Collections.EMPTY_LIST;
        }
        Example<CourseOrderDiscount> example = CourseOrderDiscount.createExample();
        example.createCriteria().andIn("courseOrderId" , courseOrderIds ).andEqualTo("discountId",discountId);
        return findListByExample(example);
    }

    @Override
    public List<CourseOrderDiscount> findByCourseOrderId(Long courseOrderId,Integer from) {
        Example<CourseOrderDiscount> example = CourseOrderDiscount.createExample();
        example.createCriteria().andEqualTo("courseOrderId" , courseOrderId ).andEqualTo("fromSrc" , from );
        return findListByExample(example);
    }

    @Override
    public void deleteByCourseOrderId(Long courseOrderId) {
        Example<CourseOrderDiscount> example = CourseOrderDiscount.createExample();
        example.createCriteria().andEqualTo("courseOrderId" , courseOrderId );
        deleteByExample(example);
    }

    @Override
    public CourseOrderDiscount findByCourseOrderIdAndDiscountId(Long courseOrderId, Long discountId) {
        Example<CourseOrderDiscount> example = CourseOrderDiscount.createExample();
        example.createCriteria().andEqualTo("courseOrderId" , courseOrderId ).andEqualTo("discountId" ,discountId );
        return findOneByExample(example);
    }

    @Override
    public Page<DiscountCodeUseDetailVo> findDiscountCodeUseDetail(CourseOrderDiscountQueryVo courseOrderDiscountQueryVo,int pageNo,int pageSize) {
        List<BusTeam> busTeamList=busTeamService.findListByExample(BusTeam.createExample());
        Map<Long, BusTeam> busTeamMap = BeanUtils.toMap(busTeamList, "busTeamId");

        List<AreaTeam> areaTeamList=areaTeamService.findAllAreaTeam();
        Map<Long,AreaTeam> areaTeamMap=BeanUtils.toMap(areaTeamList,"areaTeamId");
        Example<CourseOrderDiscount> example=CourseOrderDiscount.createExample();
        Example.Criteria criteria=example.createCriteria();
        CriteriaUtils.fillCondition(criteria,courseOrderDiscountQueryVo);
        criteria.andEqualTo("discountId",1l);
        Page<CourseOrderDiscount> courseOrderDiscountPage=this.findPageByExample(example,pageNo,pageSize);
        Page<DiscountCodeUseDetailVo> discountCodeUseDetailVoPage=Utils.newPage(courseOrderDiscountPage);

        List<CourseOrderDiscount> courseOrderDiscountList=courseOrderDiscountPage.getRecords();
        if (courseOrderDiscountList!=null){
            List<Long> courseOrderIds = BeanUtils.toList(courseOrderDiscountList, "courseOrderId");
            List<StudentReg> studentRegList=new ArrayList<>();
            List<Long> tempList=new ArrayList<>(200);
            int i=0;
            for (Long courseOrderId:courseOrderIds){
                i++;
                tempList.add(courseOrderId);
                if (i==200){
                    studentRegList.addAll(studentRegService.findByCourseOrderIds(tempList));
                    i=0;
                    tempList.clear();
                }
            }
            if (tempList.size()>0){
                studentRegList.addAll(studentRegService.findByCourseOrderIds(tempList,"busTeamId","operator","operatorId","courseOrderId"));
            }
            Map<Long, List<StudentReg>> regMap = BeanUtils.toGroup(studentRegList, "courseOrderId");
            List<DiscountCodeUseDetailVo> discountCodeUseDetailVos=new ArrayList<>(courseOrderDiscountList.size());
            for (CourseOrderDiscount courseOrderDiscount:courseOrderDiscountList){
                DiscountCodeUseDetailVo discountCodeUseDetailVo=new DiscountCodeUseDetailVo();
                discountCodeUseDetailVo.setDiscountId(courseOrderDiscount.getDiscountId());
                discountCodeUseDetailVo.setDiscountName(courseOrderDiscount.getDiscountName());
                discountCodeUseDetailVo.setDiscountCode(courseOrderDiscount.getDiscountCode());
                discountCodeUseDetailVo.setCourseOrderId(courseOrderDiscount.getCourseOrderId());
                discountCodeUseDetailVo.setDiscountAmount(courseOrderDiscount.getDiscountAmount());
                discountCodeUseDetailVo.setCreateTime(courseOrderDiscount.getCreateTime());
                List<StudentReg> oneCourseRegList=regMap.get(courseOrderDiscount.getCourseOrderId());

                AreaTeam areaTeam=areaTeamMap.get(courseOrderDiscount.getAreaTeamId());
                if (areaTeam!=null){
                    discountCodeUseDetailVo.setAreaTeamId(areaTeam.getAreaTeamId());
                    discountCodeUseDetailVo.setAreaTeamName(areaTeam.getName());
                }
                if (Utils.checkListNotEmpty(oneCourseRegList)){
                    discountCodeUseDetailVo.setOperatorId(oneCourseRegList.get(0).getOperatorId());
                    discountCodeUseDetailVo.setOperatorName(oneCourseRegList.get(0).getOperator());
                    List<Long> busTeamIds=BeanUtils.toList(oneCourseRegList,"busTeamId");
                    busTeamIds=Utils.removeDuplicate(busTeamIds);
                    discountCodeUseDetailVo.setBusTeamIds(StringUtils.join(busTeamIds,","));
                    List<String> busTeamNames=new ArrayList<>(busTeamIds.size());
                    for (Long busTeamId:busTeamIds){
                        BusTeam busTeam=busTeamMap.get(busTeamId);
                        if (busTeam!=null){
                            busTeamNames.add(busTeam.getName());
                        }
                    }
                    discountCodeUseDetailVo.setBusTeamNames(StringUtils.join(busTeamNames,","));
                }
                discountCodeUseDetailVos.add(discountCodeUseDetailVo);
            }
            discountCodeUseDetailVoPage.setRecords(discountCodeUseDetailVos);
        }
        return discountCodeUseDetailVoPage;
    }

    @Override
    public SXSSFWorkbook exportDiscountCodeUseDetail(CourseOrderDiscountQueryVo courseOrderDiscountQueryVo) {
        Page<DiscountCodeUseDetailVo> discountCodeUseDetailVoPage=this.findDiscountCodeUseDetail(courseOrderDiscountQueryVo,1,20000);
        return discountCodeUseDetail2excel(discountCodeUseDetailVoPage.getRecords());
    }


    public static SXSSFWorkbook discountCodeUseDetail2excel(List<DiscountCodeUseDetailVo> discountCodeUseDetailVoList) {
        SXSSFWorkbook excel = new SXSSFWorkbook();
        SXSSFSheet sheet = excel.createSheet("优惠卷使用概况");
        SXSSFRow firstRow = sheet.createRow(0);
        String[] titles = new String[]{ "区域", "优惠券名称", "优惠卷码","优惠券金额", "订单编号", "操作人", "使用时间", "业务组"};
        for (int i = 0; i < titles.length; i++) {
            firstRow.createCell(i).setCellValue(titles[i]);
        }
        int rowNum = 1;

        if (discountCodeUseDetailVoList!=null){
            for (DiscountCodeUseDetailVo discountCodeUseDetailVo : discountCodeUseDetailVoList) {
                int cellNum = 0;

                SXSSFRow row = sheet.createRow(rowNum++);

                SXSSFCell cell = row.createCell(cellNum++);
                cell.setCellValue(StringUtils.defaultString(discountCodeUseDetailVo.getAreaTeamName()));


                cell = row.createCell(cellNum++);
                cell.setCellValue(StringUtils.defaultString(discountCodeUseDetailVo.getDiscountName()));

                cell = row.createCell(cellNum++);
                cell.setCellValue(StringUtils.defaultString(discountCodeUseDetailVo.getDiscountCode()));

                cell = row.createCell(cellNum++);
                cell.setCellType(CellType.NUMERIC);
                cell.setCellValue(discountCodeUseDetailVo.getDiscountAmount()==null?0d:discountCodeUseDetailVo.getDiscountAmount().doubleValue());


                cell = row.createCell(cellNum++);
                cell.setCellValue(discountCodeUseDetailVo.getCourseOrderId()==null?"":discountCodeUseDetailVo.getCourseOrderId().toString());


                cell = row.createCell(cellNum++);
                cell.setCellValue(StringUtils.defaultString(discountCodeUseDetailVo.getOperatorName()));


                cell = row.createCell(cellNum++);
                cell.setCellValue(discountCodeUseDetailVo.getCreateTime()==null?"":Utils.formatDate(discountCodeUseDetailVo.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));

                cell = row.createCell(cellNum++);
                cell.setCellValue(StringUtils.defaultString(discountCodeUseDetailVo.getBusTeamNames()));

            }
        }
        return excel;
    }

}